﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using OSGeo.MapGuide;

namespace IGIS
{

    public class FeatProperty
    {
        private object mValue;

        public string Name { get; set; }
        public string Alias { get; set; }
        public int PropertyType { get; set; }
        public bool IsReadOnly { get; set; }
        public bool IsIdentity { get; set; }
        public bool IsDisplayed { get; set; }
        public bool IsUpdated { get; set; }

        public object Value
        {
            get { return mValue; }
            set
            {
                if ((mValue == null) || (mValue.ToString() != value.ToString()))
                {
                    if ((AviableValues != null) && (!AviableValues.Contains(value.ToString())))
                    {
                        throw new InvalidOperationException(string.Format("נתונים שגוים בשדה <b>{0}</b>", Alias));
                    }

                    mValue = value;
                    IsUpdated = true;
                }
            }
        }
        public List<string> AviableValues { get; set; }
        public Dictionary<int, string> LookUpValues { get; set; }
        public FeatProperty(string name, string alias)
        {
            Name = name;
            Alias = alias;
            if (Alias == "תאריך עדכון" || Alias == "עדכון אחרון")
            {
                IsUpdated = true;
            }
            else IsUpdated = false;
        }

        public void LoadValue(object val)
        {
            mValue = val;
        }

        public MgProperty GenerateProperty()
        {
            MgProperty prop = null;
            if (IsReadOnly || IsIdentity || !IsUpdated) return null;
            switch (PropertyType)
            {
                case MgPropertyType.Boolean:
                    bool val = new bool();
                    if (!bool.TryParse(Value.ToString(), out val))
                    { throw new InvalidOperationException(string.Format("<b>{0}</b> - נתון אינו תקין", Alias)); }
                    prop = new MgBooleanProperty(Name, val);
                    break;
                case MgPropertyType.Byte:
                    byte val2 = new byte();
                    if (!byte.TryParse(Value.ToString(), out val2))
                    { throw new InvalidOperationException(string.Format("<b>{0}</b> - נתון אינו תקין", Alias)); }
                    prop = new MgByteProperty(Name, val2);
                    break;
                case MgPropertyType.Double:
                    double val3 = new double();
                    if (!double.TryParse(Value.ToString(), out val3))
                    { throw new InvalidOperationException(string.Format("<b>{0}</b> חייב להיות מספר", Alias)); }
                    prop = new MgDoubleProperty(Name, val3);
                    break;
                case MgPropertyType.Int16:
                    short val4 = new short();
                    if (!short.TryParse(Value.ToString(), out val4))
                    { throw new InvalidOperationException(string.Format("<b>{0}</b> חייב להיות מספר", Alias)); }
                    prop = new MgInt16Property(Name, val4);
                    break;
                case MgPropertyType.Int32:
                    int val5 = new int();
                    if (!int.TryParse(Value.ToString(), out val5))
                    { throw new InvalidOperationException(string.Format("<b>{0}</b> חייב להיות מספר", Alias)); }
                    prop = new MgInt32Property(Name, val5);
                    break;
                case MgPropertyType.Int64:
                    long val6 = new long();
                    if (!long.TryParse(Value.ToString(), out val6))
                    { throw new InvalidOperationException(string.Format("<b>{0}</b> חייב להיות מספר", Alias)); }
                    prop = new MgInt64Property(Name, val6);
                    break;
                case MgPropertyType.DateTime:
                    MgDateTime dt = new MgDateTime();
                    short day, month, year;
                    if (Alias == "תאריך עדכון" || Alias == "עדכון אחרון")
                    {
                        Value = DateTime.Now.ToString("dd/MM/yyyy");
                    }
                    string[] arrDT = Value.ToString().Split('/');
                    if (!short.TryParse(arrDT[0], out day) || !short.TryParse(arrDT[1], out month) || !short.TryParse(arrDT[2], out year))
                    { throw new InvalidOperationException(string.Format("<b>{0}</b> פורמת תאריך שגוי", Alias)); }
                    dt.Day = day;
                    dt.Month = month;
                    dt.Year = year;
                    prop = new MgDateTimeProperty(Name, dt);
                    break;
                case MgPropertyType.String:
                    string val7 = "";
                    val7 = Value.ToString();
                    prop = new MgStringProperty(Name, val7);
                    break;
                default:
                    break;
            }
            return prop;
        }
    }

}